package github.leavesczy.reactivehttpsamples.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.CollectionUtils
import com.chad.library.adapter4.BaseQuickAdapter
import github.leavesczy.reactivehttpsamples.core.mode.MeasureModelData
import github.leavesczy.reactivehttpsamples.core.mode.ResulData
import github.leavesczy.reactivehttpsamples.databinding.AdapterSampleSelectItemBinding
import github.leavesczy.reactivehttpsamples.util.Logger

class BridgeSampleSelectAdapter : BaseQuickAdapter<MeasureModelData, BridgeSampleSelectAdapter.VH>(),
    BaseQuickAdapter.OnItemClickListener<MeasureModelData> {

    class VH(val bind: AdapterSampleSelectItemBinding) : RecyclerView.ViewHolder(bind.root)

    init {
        setOnItemClickListener(this)
    }

    private val selectIds= mutableListOf<Long>()
    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): BridgeSampleSelectAdapter.VH {
        val inflate =
            AdapterSampleSelectItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return VH(inflate)
    }

    override fun onBindViewHolder(holder: VH, position: Int, item: MeasureModelData?) {
        item?.let {
            holder.bind.rvSampleName.text=it.fileName
            val contains = selectIds.contains(it.id)
            Logger.d("contains--${contains}")
            holder.bind.cbSampleSelect.isChecked=contains
        }
    }

    override fun onClick(adapter: BaseQuickAdapter<MeasureModelData, *>, view: View, position: Int) {
        val item = getItem(position)
        item?.let {
            if(selectIds.contains(it.id)){
                selectIds.remove(it.id)
            }else{
                selectIds.add(it.id)
            }
            notifyItemChanged(position)
        }
    }


    fun initSelectIds(selectIds:List<Long>?){
        if(!CollectionUtils.isEmpty(selectIds)){
            this.selectIds.clear()
            this.selectIds.addAll(selectIds!!)
        }
    }

    fun getSelectIds()=selectIds
}


